<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .nav {
            width: 50px;
            height: 50px;
            background-color: red;
            margin: 200px auto;
            border-radius: 50%;
        }
    </style>
</head>

<body>
    <div class="nav">

    </div>
    <script>
        const nav = document.querySelector('.nav')
        // 随机颜色
        // 1. 随机生成一个十六进制的颜色
        // 2. 随机生成一个rgb的颜色
        // 3. 随机生成一个颜色
        function getRandomColor(flag) {
            if (flag) {
                let str = '#'
                let arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                    'a', 'b', 'c', 'd', 'e', 'f']
                for (let i = 1; i <= 6; i++) {
                    let num = Math.floor(Math.random() * arr.length)
                    str += arr[num]
                }
                return str
            }
            else {
                let r = Math.floor(Math.random() * 256)
                let g = Math.floor(Math.random() * 256)
                let b = Math.floor(Math.random() * 256)
                return `rgb(${r},${g},${b})`
            }
        }
        const div = document.querySelector('div')
        div.style.backgroundColor = getRandomColor(true)
        setInterval(function () {
            div.style.backgroundColor = getRandomColor(false)
        }, 50);
        // nav.classList.remove('nav')
        // nav.classList.toggle('cd')
    </script>
</body>

</html>